<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>眼睛跟随鼠标</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .panda {
            width: 200px;
            height: 200px;
            border: 4px #000 solid;
            border-radius: 100%;
            margin: 300px auto;
            position: relative;
        }

        .eyes {
            display: flex;
            gap: 100px;
        }

        .eye {
            width: 50px;
            height: 50px;
            border: 1px #000 solid;
            border-radius: 100%;
            position: relative;
            background-color: #fff;
            ;
        }

        .eye-pointer {
            width: 30px;
            height: 30px;
            background-color: #000;
            border-radius: 100%;
            position: absolute;
        }

        .mouse {
            width: 50%;
            height: 40%;
            border-radius: 100%;
            border: 1px #000 solid;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            overflow: hidden;

        }

        .mouse::before {
            content: "";
            display: block;
            width: 100%;
            height: 60%;
            background-color: #000;
            position: absolute;
            top: 0;
            left: 0;
            border-bottom-left-radius: 50%;
            border-bottom-right-radius: 50%;
        }
    </style>
</head>

<body>
    <div class="panda">
        <div class="eyes">
            <div class="eye">
                <div class="eye-pointer" id="leftEye"></div>
            </div>
            <div class="eye">
                <div class="eye-pointer" id="rightEye"></div>
            </div>
        </div>
        <div class="mouse"></div>
    </div>
</body>
<script>
    const leftEye = document.getElementById('leftEye');
    const rightEye = document.getElementById('rightEye');
    const R = 10;//半径 (50 - 30)/2
    function eyeAnimation(event, div) {
        const x = event.clientX;
        const y = event.clientY;
        const centerX = window.innerWidth / 2;
        const centerY = window.innerHeight / 2;

        // 计算鼠标位置与中心点的角度
        const angle = Math.atan2(y - centerY, x - centerX) * (180 / Math.PI);


        var radians = angle * (Math.PI / 180);
        div.style.left = R * Math.cos(radians) + R + 'px';
        div.style.top = R * Math.sin(radians) + R + 'px';
    }
    document.addEventListener('mousemove', function (event) {
        eyeAnimation(event, leftEye)
        eyeAnimation(event, rightEye)
    });
</script>

</html>